package com.xiangzi.xzschedule.quartz;

import java.util.Date;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

import com.xiangzi.xzschedule.job.HelloJob;

public class QuartzTest {

	public static void main(String[] args) throws InterruptedException {

		try {
			// Grab the Scheduler instance from the Factory
			Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

			// and start it off
			scheduler.start();

			// define the job and tie it to our HelloJob class
			JobDetail job = JobBuilder.newJob(HelloJob.class)
					.withIdentity("job1", "group1").build();

			// compute a time that is on the next round minute
			Date runTime = new Date();
			
			// Trigger the job to run on the next round minute
			Trigger trigger = TriggerBuilder.newTrigger()
					.withIdentity("trigger1", "group1").startAt(runTime)
					.build();

			// Tell quartz to schedule the job using our trigger
			scheduler.scheduleJob(job, trigger);
			
			//Wait for 90 seconds to give Quartz a chance to run the job
			Thread.sleep(90L * 1000L);
			
			//scheduler.shutdown();
			scheduler.shutdown(true);
			
		} catch (SchedulerException se) {
			se.printStackTrace();
		}
	}

}
